package com.idudiao.sc.common.core.validator;

import cn.hutool.core.util.StrUtil;
import com.idudiao.sc.common.core.validator.constraints.Phone;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 手机号验证逻辑
 *
 * @author: 读钓
 * @date 2018-12-04 下午 2:41
 */
public class PhoneValidator implements ConstraintValidator<Phone, String> {

    /**
     * 手机号验证 正则表达式
     */
    private static final Pattern PHONE_PATTERN = Pattern.compile(
            "^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\\d{8}$"
    );

    @Override
    public void initialize(Phone constraintAnnotation) {

    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (StrUtil.isBlank(value)) {
            return false;
        }
        Matcher m = PHONE_PATTERN.matcher(value);
        return m.matches();
    }
}